//
//  SYJComposeToolBar.swift
//  weibo
//
//  Created by 孙英建 on 16/4/23.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class SYJComposeToolBar: UIView {
    
    var emotionButton: UIButton?
    
    // 表情键盘和系统键盘的图标切换
    var isEmoticon: Bool = false{
        didSet{
            if isEmoticon{
                // 当前为表情键盘 展示系统键盘图片
                emotionButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                emotionButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }else{
                // 当前为系统键盘 展示表情键盘图片
                emotionButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                emotionButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)  
            }
        }
    }
    
    
    // 定义一个闭包将点击传到控制器
    var composeToolBarClosure:((tag: Int) -> ())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    // 设置UI控件
    func setUpUI(){
        self.backgroundColor = UIColor.orangeColor()
        
        // 添加底部按钮
        let pictureButton = addChildButton("compose_toolbar_picture", tag: 1)
        let mentionButton = addChildButton("compose_mentionbutton_background", tag: 2)
        let trendButton = addChildButton("compose_trendbutton_background", tag: 3)
        emotionButton = addChildButton("compose_emoticonbutton_background", tag: 4)
        let addButton = addChildButton("compose_add_background", tag: 5)
        
        // 添加约束
        pictureButton.snp_makeConstraints { (make) -> Void in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        
        mentionButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        
        trendButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emotionButton!)
        }
        
        emotionButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        
        addButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emotionButton!.snp_trailing)
        }
        
    }
    
    // 按钮的添加方法
    func addChildButton(imgName: String, tag: Int) -> UIButton{
        let btn = UIButton()
        
        btn.tag = tag
        
        btn.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        // 按钮的点击事件
        btn.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
       
        addSubview(btn)
        return btn
    }
    
    
    // MARK: - 按钮的点击事件
    @objc private func buttonClick(sender: UIButton){
        composeToolBarClosure?(tag: sender.tag)
    }
    
    
}















